Thread: [Newbie] Factorial

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    14

    [Newbie] Factorial

    Hi, I'm doing my homework and I'm stuck on this factorial part. I know this is the code for n! from my text:

    Code:
    #include <stdio.h>
    double  mfact(int);
    main()
    {
               int m;
               printf("Enter an integer m>0: ");
               scanf("%d",&m);
               printf("m! is: %g\n",mfact(m));
    }
    double mfact(int m)
    {
        if(m==1)return 1;
        else return m*mfact(m-1);
    }
    But I need 2n! instead so I tried to modify the code to this:

    Code:
    #include <stdio.h>
    double  mfact(int);
    main()
    {
               int m;
               printf("Enter an integer m>0: ");
               scanf("%d",&m);
               printf("m! is: %g\n",mfact(m));
    }
    double mfact(int m)
    {
        if(m==1)return 2;
        else return 2m*mfact(2m-1);
    }
    Because I figured that 2n! = 2n*(2n-1) ... etc. so it would work but it's messed up. So in fact, I realized I don't get what this whole code is about. What is the mfact specifically?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I note that (2n)! = m!, where m = 2n.

    I realized I don't get what this whole code is about. What is the mfact specifically?
    mfact is a recursive function.
    Last edited by laserlight; 05-13-2007 at 02:48 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    14
    Thanks for the help. I don't know why but I'm still getting the wrong answer because when I entered in 2, I get 48 when I'm supposed to get 24.

    Code:
    #include <stdio.h>
    double  mfact(int);
    main()
    {
               int m, n;
               printf("Enter an integer m>0: ");
               scanf("&#37;d",&n);
    	     m = 2*n;
               printf("2n! is: %g\n",mfact(m));
    }
    double mfact(int m)
    {
    if(m==2)return 2;
    else return m*mfact(m-1);
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Aside from the fact that mfact(1) no longer works, and that your code formatting could be improved, it looks okay to me. You do not actually need another variable in main() though, since n can be reused.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    14
    Thanks! I got it now!

    Edit: Just a quick question but is it possible to put m = 2n; like this:

    Code:
    double mfact(int m)
    {
    m = 2n;
    if(m==2)return 2;
    else return m*mfact(m-1);
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You should just write mfact() to compute the factorial of any integer >= 0. Then just plug in 2*n when you call it. You're not going to accomplish or simplify anything by trying to change the body of the function itself. BTW, if you replaced "if (m == 1)" with "if (m == 0)" you could have it handle the case 0! == 1.

  7. #7
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Because I figured that 2n! = 2n*(2n-1) ... etc. so it would work but it's messed up.
    Typically, if that's truly what was meant, it would be written (2n)!. If that is the case,do what robatino said.

    But to me it'd be more likely to ask something like finding 2*(n!).
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by lazyturtle View Post
    Thanks! I got it now!

    Edit: Just a quick question but is it possible to put m = 2n; like this:

    Code:
    double mfact(int m)
    {
    m = 2n;
    if(m==2)return 2;
    else return m*mfact(m-1);
    }
    Where did n come from?

    if you mean m = 2m... m *= 2;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault using recursion to find factorial
    By kapok in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2009, 11:10 AM
  2. Factorial
    By foxman in forum Contests Board
    Replies: 27
    Last Post: 07-11-2008, 06:59 PM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  4. Basic Factorial from 1-10
    By AaA in forum C Programming
    Replies: 20
    Last Post: 05-28-2005, 07:39 AM
  5. factorial output
    By maloy in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 03:28 PM